define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'microend/auther/index',
                    add_url: 'microend/auther/add',
                    edit_url: 'microend/auther/edit',
                    del_url: 'microend/auther/del',
                    multi_url: 'microend/auther/multi',
                    ajax_url: {
                        btn_1: {
                            data: {'type': "2"}, url: "microend/auther/update_status", fc: function (data) {
                                return true;
                            }
                        },
                        btn_2: {
                            data: {'type': "3"}, url: "microend/auther/update_status", fc: function (data) {
                                return true;
                            }
                        }
                    },
                    table: 'auth_real',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'auth_id',
                sortName: 'auth_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'username', title: __('Username'),operate: 'like'},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'auth_no', title: __('Auth_no')},
                        {field: 'auth_img', title: __('Auth_img'),formatter: Controller.api.formatter.thumb, operate: false},
                        {field: 'auth_status', title: __('身份证审核状态'),searchList: {"1":"认证中","2":"认证通过","3":"认证失败"},
                            //operate:'BETWEEN',
                            formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                var index = keys.indexOf(value);
                                value = value === null ? '未提交' : value.toString();
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                                if (this.operate != false) {
                                    html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                                }
                                return html;
                            }},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter:function(value,row,index){

                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);
                                if(row.auth_status==1){
                                    buttons.push(
                                        {
                                            name: 'btn_1',
                                            text: "审核",
                                            extend: 'data-toggle="tooltip" data-name="btn_1"',
                                            classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                        },
                                        {
                                            name: 'btn_2',
                                            text: "驳回",
                                            extend: 'data-toggle="tooltip" data-name="btn_2"',
                                            classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                        }
                                    );
                                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                                }

                            }
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

            //全部通过按钮
            $(document).on("click", "#mult_pass", function (e){
                var data = table.bootstrapTable('getSelections');
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部通过？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'microend/auther/mult_pass',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });

            //全部驳回按钮
            $(document).on("click", "#mult_no", function (e){
                var data = table.bootstrapTable('getSelections');
                console.log(data);
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定全部驳回？',{
                        btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'microend/auther/mult_no',
                            data: {
                                'ids': data,

                            },
                            success: function (data) {
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            },
                            error: function (data) {
                                //失败的回调
                                layer.msg(data.msg);
                                table.bootstrapTable("refresh");
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    if(row.auth_img) {
                        var style = row.storage == 'upyun' ? '!/fwfh/120x90' : '';
                        return '<a href="' + row.auth_img + '" target="_blank"><img src="' + row.auth_img + style + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    }
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + value + '</a>';
                },
            }
        }
    };
    return Controller;
});